package br.com.futibamanager.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import br.com.futibamanager.model.User;

/**
 * An utility class for e-mail sending.
 * 
 * @author diogenes.feijo
 *
 */
public abstract class EmailSender {
	
	private static final String EMAIL_PROPERTIES = "email.properties";
	
	/**
	 * Sends an e-mail to recently registered user.
	 * 
	 * @param user - the registered user
	 */
	public static void sendRegisteredUserEmail(User user) {
		
		Map<String, String> parametersMap = new HashMap<String, String>();
		
		parametersMap.put("name", user.getName());
		parametersMap.put("login", user.getLogin());
		parametersMap.put("password", user.getPassword());
		
		String message = 
			TemplateManager.getTemplate(TemplateManager.TemplateNames.REGISTERED_USER_EMAIL, parametersMap);
		
		send(FacesUtil.getMessage("registered.user.email.subject"), message, user.getEmail());
	}
	
	/**
	 * Sends an e-mail to the user with its registry data.
	 * 
	 * @param user - the registered user
	 */
	public static void sendUserDataEmail(User user) {
		
		Map<String, String> parametersMap = new HashMap<String, String>();
		
		parametersMap.put("name", user.getName());
		parametersMap.put("login", user.getLogin());
		parametersMap.put("password", user.getPassword());
		
		String message = 
			TemplateManager.getTemplate(TemplateManager.TemplateNames.USER_RECOVERY_DATA_EMAIL, parametersMap);
		
		send(FacesUtil.getMessage("user.data.request.email.subject"), message, user.getEmail());
	}
	
	/**
	 * Sends an e-mail to the given recipients.
	 * 
	 * @param subject - the subject
	 * @param message - the message
	 * @param recipients - the recipients separated by comma
	 */
	private static void send(String subject, String message, String recipients) {
		
		try {
			final Properties properties = new Properties();
			
			properties.load(
				Thread.currentThread().getContextClassLoader().getResourceAsStream(EMAIL_PROPERTIES));
			
			Authenticator authenticator = new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(properties.getProperty("mail.username"), 
													  properties.getProperty("mail.password"));
				}
			};
	
			Session session = Session.getInstance(properties, authenticator);
			
			MimeMessage mimeMessage = new MimeMessage(session);

			mimeMessage.setSubject(subject);
			mimeMessage.setRecipients(RecipientType.TO, InternetAddress.parse(recipients, false));
			mimeMessage.setDataHandler(new DataHandler(message, properties.getProperty("mail.mime.type")));
			mimeMessage.setFrom(new InternetAddress(properties.getProperty("mail.sender")));
			mimeMessage.saveChanges();
	
			Transport.send(mimeMessage);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
